package com.logistics.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.logistics.Express;
import com.logistics.ExpressQueryParam;
import com.logistics.admin.DateUtils;

import cn.easybuild.core.hibernate.StringPojoHibernateHandler;

@Repository
public class ExpressDaoImpl extends StringPojoHibernateHandler<Express> implements ExpressDao {

	@Override
	public List<Express> query(final ExpressQueryParam param) {
		return queryWithParameter(param, "e", "e.createDate desc", new QueryCriteriaSetter() {

			@Override
			public void setCriteria(StringBuffer hql, Map<String, Object> values) {
				if (StringUtils.isNotBlank(param.getNumber())) {
					hql.append(" and number = :number");
					values.put("number", param.getNumber());
				}
				
				if (StringUtils.isNotBlank(param.getStartDate())) {
					hql.append(" and createDate >= :startDate");
					values.put("startDate",DateUtils.parseDateStratTime(param.getStartDate()));
				}
				
				if (StringUtils.isNotBlank(param.getEndDate())) {
					hql.append(" and createDate <= :endDate");
					values.put("endDate", DateUtils.parseDateEndTime(param.getEndDate()));
				}
			}
		});
	}

	@Override
	protected Class<Express> getEntityClass() {
		return Express.class;
	}

	@Override
	public List<Express> needPrints() {
		StringBuffer buffer = new StringBuffer(" from ");
		buffer.append(getEntityNameForQuery());
		buffer.append(" where print = ?");
		
		return query(buffer.toString(), true);
	}

}
